<script setup lang="ts">
import {markRaw, onMounted, ref} from "vue";
import type {EChartsOption, EChartsType} from "echarts";
import * as echarts from "echarts";
import {useGetBarDataApi} from "@/api";
import 'echarts-wordcloud'
import type {Bar} from "@/interface";

const bar = ref()
const option = ref<EChartsOption>()
const myChart = ref<EChartsType>()
const barData = ref<Bar>({
  name: [],
  value:[]
})
const init = async () => {
  const {data: res} = await useGetBarDataApi()
  barData.value = res
  myChart.value = markRaw(echarts.init(bar.value));
  draw();
}
const draw = () => {
  option.value = {
    title: [
      {
        left: 'center',
        text: '职位名称词频TOP5',
        textStyle:{
          color:'#000000'
        }
      }
    ],
    tooltip:{},
    xAxis: {
      type: 'category',
      data: barData.value.name,
      axisLabel:{
        color:'#ffffff'
      },
      axisLine:{
        lineStyle:{
          color:'#ffffff'
        }
      }
    },
    yAxis: {
      type: 'value',
      axisLabel:{
        color:'#ffffff'
      }
    },
    series: [
      {
        data: barData.value.value,
        type: 'bar',
        showBackground: true,
        backgroundStyle: {
          color: 'rgba(180, 180, 180, 0.2)'
        }
      }
    ]
  }
  myChart.value.setOption(option.value, {notMerge: true});
}
onMounted(() => {
  init()
})
</script>

<template>
  <div ref="bar" class="bar">
  </div>
</template>

<style scoped>
.bar {
  width: 100%;
  height: 100%;
}
</style>